<template>
  <div class="container knowledge-page">
    <div class="page-tab">
      <Tab :list="tabList" v-model:value="tab"></Tab>
      <el-upload
              class="import"
              :action="uploadUrl"
              :headers="uploadHeaders"
              :show-file-list="false"
              :auto-upload="true"
              :on-success="handleUploadSuccess"
              :on-error="handleUploadError"
              :data="{}"
              accept="*"
              :before-upload="beforeUpload"
      >
        <div class="import">
          <div class="icon">
            <span class="iconfont icon-shangchuan1"></span>
          </div>
          <span class="text">{{$t('导入内容')}}</span>
        </div>
      </el-upload>
    </div>
    <div class="wrapper">
      <div class="wrapper-part" v-if="tab === 'event'">
        <el-scrollbar>
          <div class="scroll-wrapper">
            <Doc :refresh="refresh"></Doc>
          </div>
        </el-scrollbar>
      </div>
    </div>
  </div>
</template>

<script>
  import { ElLoading, ElMessage } from 'element-plus';
  import Tab from '@/components/tab';
  import Doc from './doc.vue';
  export default {
    name: 'index',
    components: {
      Tab,
      Doc,
    },
    data() {
      return {
        uploadUrl: `${process.env.VUE_APP_BASE_API}yswn/WnKnowledge`,
        uploadHeaders: {
          Token: `${localStorage.getItem('token')}`,
        },
        tab: 'event',
        tabList: [
          {
            title: this.$t('文档'),
            value: 'event',
          },
        ],
        refresh: false,
        loading: null,
      };
    },
    methods: {
      // 上传前设置 loading 状态
      beforeUpload() {
        this.loading = ElLoading.service({
          lock: true,
          text: this.$t('Loading'),
          background: 'rgba(0, 0, 0, 0.7)',
        });
      },
      // 处理上传成功
      handleUploadSuccess(response, file, fileList) {
        if (this.loading) {
          this.loading.close();
        }
        if (response.code === 0) {
          ElMessage({
            type: 'success',
            message: this.$t('文件上传成功!'),
          });
          this.refresh = true;
        }
      },
      // 处理上传失败
      handleUploadError(err, file, fileList) {
        if (this.loading) {
          this.loading.close();
        }
        ElMessage({
          type: 'error',
          message: this.$t('文件上传失败!'),
        });
      },
    },
  };
</script>
<style scoped lang="scss">
  .container {
    width: 100%;
    height: calc(100% - 60px);
    background: #181818;
    box-shadow: 12px 12px 30px 0px rgba(40, 58, 44, 0.08);
    border-radius: 32px 32px 32px 32px;
    overflow: hidden;
    .box {
      width: 710px;
      height: 232px;
      background: linear-gradient(
                      24deg,
                      #0f0f0f 0%,
                      rgba(54, 108, 90, 0.71) 100%
      );
      box-shadow: 20px 20px 30px 0px rgba(0, 0, 0, 0.65);
      border-radius: 32px 32px 32px 32px;
      border: 1px solid;
      border-image: linear-gradient(
                      17deg,
                      rgba(24, 24, 24, 1),
                      rgba(31, 210, 103, 1)
      )
      1 1;
    }
    .page-tab {
      width: 100%;
      box-sizing: border-box;
      padding: 20px 25px;
      position: relative;
      z-index: 10;
      box-shadow: 0 15px 15px 0 rgba(#181818, 0.5);
      display: flex;
      justify-content: space-between;
      align-items: center;
      &:before {
        content: '';
        width: calc(100% - 50px);
        left: 25px;
        position: absolute;
        left: 25px;
        bottom: 0px;
        height: 1px;
        background: #3b3b3b;
      }
    }
    .wrapper {
      width: 100%;
      height: calc(100% - 100px);
      .wrapper-part {
        width: 100%;
        height: 100%;
        .scroll-wrapper {
          box-sizing: border-box;
          padding: 0px 40px;
        }
      }
    }
    .import {
      width: 176px;
      height: 50px;
      background: #303f2e;
      border-radius: 30px;
      display: inline-flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      transition: 0.3s ease;
      .icon {
        width: 32px;
        height: 32px;
        background: $color;
        color: #fff;
        text-align: center;
        line-height: 32px;
        border-radius: 50%;
        margin-right: 10px;
      }
      .text {
        font-weight: 400;
        font-size: 16px;
        color: #ffffff;
        transition: 0.3s ease;
      }

      &:hover {
        background: #fff;
        .text {
          color: $color;
        }
      }
    }
  }
</style>

